<template>
  <div class="page-box">
    <universal-table :columns="columns" :fetch-data="fetchTableData" permission-prefix="order:neighbor_worker" :operationWidth="operationWidth"
      :show-selection="true" :show-index="false" :export-file-name="'订单'" :init-search-form="searchForm" @edit="handleEdit"
      @delete="handleDelete" @reset="reset" @status-change="handleStatusChange" :bt1Name="'查看'" ref="table">

      <!-- 操作扩展 -->
      <template slot="operationExtend" slot-scope="{ row }">
        <el-button size="mini" type="success" :row=row @click="handleActionApprove(row)"
          v-if="permission.includes('order:neighbor_worker:edit') && row.status==7 && !row.neighbor_worker_id">分配
        </el-button>
      </template>

      <template slot="operationExtend2" slot-scope="{ row }">
        <el-button size="mini" type="danger" :row=row @click="openAfterSales(row)"
          v-if="permission.includes('order:neighbor_worker:edit') && row.afterSales_status==0">售后
        </el-button>
      </template>

      <!-- 搜索区域扩展 -->
      <template #searchExtend>
        <el-form-item prop="status">
          <el-select v-model="searchForm.status" placeholder="订单状态" clearable>
            <el-option v-for="item in approvedOptions" :key="item.id" :label="item.name" :value="item.id"></el-option>
          </el-select>
        </el-form-item>
      </template>

      <template #searchExtend2>
        <el-form-item prop="afterSalesStatus">
          <el-select v-model="searchForm.afterSalesStatus" placeholder="售后状态" clearable>
            <el-option v-for="item in afterSalesOptions" :key="item.id" :label="item.name" :value="item.id"></el-option>
          </el-select>
        </el-form-item>
      </template>
    </universal-table>

    <!-- 详情弹窗 -->
    <el-dialog title="订单详情" :visible.sync="dialogVisible" width="40%">
      <ReviewDetail v-if="currentRow" :formData="currentRow" @cancel_order="handleApprove" />
    </el-dialog>

    <!-- 分配弹窗 -->
    <el-dialog title="分配订单" :visible.sync="dialogAssigningVisible" width="55%">
      <AssigningDetail v-if="currentRow" :formData="currentRow" @assigning_order="handleAssigning" />
    </el-dialog>

    <!-- 售后 -->
    <el-dialog title="订单售后" :visible.sync="dialogServiceOrderAfterSalesVisible" width="55%">
      <ServiceOrderAfterSales v-if="currentRow" :formData="currentRow" @afterSales="handleAfterSales" />
    </el-dialog>

  </div>
</template>

<script>
  import {
    mapGetters
  } from "vuex";
  import UniversalTable from "@/components/TableComponent.vue";
  import ReviewDetail from "./ReviewDetail.vue";
  import AssigningDetail from "./AssigningDetail.vue";
  import ServiceOrderAfterSales from "./ServiceOrderAfterSales.vue";
  import {
    NEIGHBOR_WORKER_ORDER_STATUS,YES_NO,SERVICE_ORDER_AFTER_SALES_STATUS,SEX
  } from '@/constants/statusMap';
  export default {
    components: {
      UniversalTable,
      ReviewDetail,
      AssigningDetail,
      ServiceOrderAfterSales
    },
    data() {
      return {
        NEIGHBOR_WORKER_ORDER_STATUS,
        YES_NO,
        SEX,
        SERVICE_ORDER_AFTER_SALES_STATUS,
        dialogVisible: false,
        dialogAssigningVisible: false,
        dialogServiceOrderAfterSalesVisible: false,
        currentRow: null,
        operationWidth:'300',
        //'状态：0=未审核，1=审核通过，2=审核不通过，3=取消售后',
        afterSalesOptions: [{
            id: 999,
            name: "售后状态"
          },
          {
            id: 0,
            name: "未审核"
          },
          {
            id: 1,
            name: "审核通过"
          },
          {
            id: 2,
            name: "审核不通过"
          },
          {
            id: 3,
            name: "取消售后"
          },


        ],
        columns: [{
            prop: "id",
            label: "ID"
          },
          {
            prop: "order_no",
            label: "订单号"
          },
          {
            prop: "goodsType_name",
            label: "商品类型名称"
          },
          {
            prop: "user_phone_number",
            label: "用户账号"
          },
          {
            prop: "neighborWorker_real_name",
            label: "邻工姓名"
          },
          {
            prop: "neighborWorker_sex",
            label: "邻工性别",
            tag: true,
            tagConfig: SEX
          },
          {
            prop: "deliveryAddress_address",
            label: "收件地址"
          },
          {
            prop: "pickupAddress_address",
            label: "取件地址"
          },
          {
            prop: "order_price",
            label: "订单金额"
          },
          {
            prop: "total_price",
            label: "订单总金额"
          },
          {
            prop: "is_grab_order_hall",
            label: "是否抢单",
            tag: true,
            tagConfig: YES_NO
          },
          {
            prop: "deliver_time",
            label: "送达时间"
          },
          {
            prop: "create_time",
            label: "创建时间"
          },
          {
            prop: "afterSales_status",
            label: "售后状态",
            tag: true,
            tagConfig: SERVICE_ORDER_AFTER_SALES_STATUS
          },
          {
            prop: "nwo_status",
            label: "订单状态",
            tag: true,
            tagConfig: NEIGHBOR_WORKER_ORDER_STATUS
          },

        ],


        approvedOptions: [{
            id: 999,
            name: "全部"
          },
          {
            id: 0,
            name: "待支付"
          },
          {
            id: 1,
            name: "待服务"
          },
          {
            id: 2,
            name: "待评价"
          },
          {
            id: 3,
            name: "已完成"
          },
          {
            id: 4,
            name: "已取消"
          },
          {
            id: 5,
            name: "已售后"
          },

        ],
        searchForm: {
          afterSalesStatus:null,
          status:null

        },
      };
    },
    computed: {
      ...mapGetters(["permission"]),
    },
    methods: {
      openAfterSales(row){
        this.currentRow = {
          ...row
        }; // 可深拷贝
        this.dialogServiceOrderAfterSalesVisible = true;
      },
      //分配邻工
      handleActionApprove(row) {
        this.currentRow = {
          ...row
        }; // 可深拷贝
        this.dialogAssigningVisible = true;
      },
      // 获取表格数据
      async fetchTableData(params = {}) {
        const postData = Object.assign({}, params, this.searchForm);
        try {
          const result = await this.$httpRequest.get('/NeighborWorker/list', postData)
          console.log('-------fetchTableData----', result)
          return result
        } catch (e) {
          this.$message.error(e.message)
          throw e // 保持错误继续向上传递
        }

      },
      //重置
      reset() {
        this.searchForm.approved_status = null;

      },
      // 编辑
      handleEdit(row) {
        // 打开编辑对话框逻辑
        this.currentRow = {
          ...row
        }; // 可深拷贝
        this.dialogVisible = true;
      },

      // 删除
      async handleDelete(row) {
        console.log("删除:", row);
      },

      // 状态变更
      async handleStatusChange(row) {
        console.log("状态变更:", row);
        this.$message.success("状态更新成功");
      },
      //分配订单
      handleAssigning(payload) {
        console.log("handleAssigning信息：", payload);

        this.$httpRequest.post('/NeighborWorker/assignMaster', payload).then(result => {
          console.log('--approved---result------', result)
          if (result.code == 0) {
            this.$message.success('订单分配成功')
            this.dialogAssigningVisible = false;
            this.$refs.table.refresh()
          } else {
            this.$message.error('订单分配失败')
          }
        }).catch(e => {
          this.$message.error(e.message)
        });
      },
      //售后
      handleAfterSales(body) {

        console.log('---handleAfterSales-body--', body)
        this.$httpRequest.post('/NeighborWorker/reviewAfterSales', body).then(result => {
          console.log('--afterSalesOrder--result------', result)
          if (result.code == 0) {
            this.$message.success('售后处理成功')
            this.$refs.table.refresh()
            this.dialogServiceOrderAfterSalesVisible = false;

          } else {
            this.$message.error('售后处理失败')
          }
        }).catch(e => {
          this.$message.error(e.message)
        });
      },



      //取消订单
      handleApprove(payload) {
        console.log("信息：", payload);

        this.$httpRequest.post('/order/cancel', payload).then(result => {
          console.log('--approved---result------', result)
          if (result.code == 0) {
            this.$message.success('取消成功')
            this.$refs.table.refresh()
          } else {
            this.$message.error('取消失败')
          }
        }).catch(e => {
          this.$message.error(e.message)
        });
        this.dialogVisible = false;
      },

    },
  };
</script>

<style>
  .page-box {
    padding: 20px;

  }
</style>
